<template>
  <div id="ATable">
    <div class="ATable-btn">
      <el-row class="mb-4">
        <el-button type="primary" plain @click="useradd">新增</el-button>
        <el-button type="primary" plain>导入</el-button>
        <el-button type="primary" plain>导出</el-button>
        <el-input v-model="userSearch" placeholder="输入关键词" clearable />
        <el-button type="primary" plain @click="load">查询</el-button>
      </el-row>
    </div>
    <div class="ATable-content">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="用户ID" width="150" />
        <el-table-column prop="avatar" label="头像" width="150">
          <template #default="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                :initial-index="1"
              >
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="150" />
        <el-table-column prop="sex" label="性别" width="150" />
        <el-table-column prop="roles" label="角色" width="150" />
        <el-table-column prop="address" label="地址" width="150">
          <template #default="scope">
            <el-button
              type="primary"
              plain
              @click="getUserAddress(scope.row.address)"
              style="width: 130px"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button
              type="primary"
              plain
              @click="userEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="primary"
              plain
              @click="userDelete(scope.$index, scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--      地址查询对话框-->
      <el-dialog v-model="dialogVisible2" title="地址" width="30%">
        <span>{{ userAddress }}</span>
        <template #footer>
          <span class="dialog-footer">
            <el-button
              type="primary"
              @click="dialogVisible2 = false"
              style="width: 80px"
              >确定</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>

    <div class="page">
      <div class="demo-pagination-block">
        <span class="demonstration"></span>
        <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>

        <!--新建、修改对话框-->
        <el-dialog v-model="dialogVisible" title="提示" width="30%">
          <el-form ref="formRef" :model="userform" label-width="120px">
            <el-form-item label="头像">
              <el-upload
                action="/api/files/upload"
                :headers="headers"
                :limit="1"
                :on-success="fileUploadSuccess"
              >
                <el-button
                  type="primary"
                  plain
                  style="
                    margin-left: 20px;
                    width: 300px;
                    background-color: white;
                    color: grey;
                    border-color: darkgrey;
                  "
                >
                  上传图片
                </el-button>
              </el-upload>
              <!--              <el-input v-model="userform.avatar"></el-input>-->
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="userform.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="userform.password"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="userform.sex">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="角色">
              <el-input v-model="userform.roles"></el-input>
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="userform.address"></el-input>
            </el-form-item>
          </el-form>
          <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogVisible = false">取消</el-button>
              <el-button type="primary" @click="usersave">确定</el-button>
            </span>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: 'User',

  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: '',
      pageSize: 10,
      userSearch: '',
      dialogVisible: false,
      userform: {},
      dialogVisible2: false,
      userAddress:''
    }
  },

  computed:{
    headers(){
      return{
        'token':sessionStorage.getItem("token")
      }
    }
  },

  created() {
    this.load()
  },

  methods: {
    getUserAddress(address){
      this.userAddress = address
      this.dialogVisible2 = true
    },

    fileUploadSuccess(res) {
      // console.log("res",res)
      if(res.code == 200 || res.code == 0){
        this.userform.avatar = res.data
      }
    },

    load() {
      request.get("/user", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.userSearch
        }

      }).then(res => {
        this.tableData = res.data.records,
            this.total = res.data.total
      })
    },
    userEdit(index, row) {
      this.userform = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    userDelete(index, id) {
      request.delete("/user/delete", {
        params: {
          id
        }
      }).then(res => {
        this.load()
      })
    },

    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },

    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },

    useradd() {
      this.dialogVisible = true
      this.userform = {}
    },

    usersave() {
      let tmpRoles = this.userform.roles.toString()
      this.userform.roles = tmpRoles.split(',')
      if (this.userform.id) {  // 更新
        request.put("/user/update", this.userform).then(res => {
          console.log(res)
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      } else {  // 新增
        request.post("/user/save", this.userform).then(res => {
          console.log(res)
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }
    }

  }

}
</script>

<style lang="less" scoped>
@import "../../style/admin/table.less";
</style>
